package com.config;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
 * WebSocketConfig配置测试
 */
@ExtendWith(MockitoExtension.class)
public class WebSocketConfigTest {

    @Test
    void testServerEndpointExporter() {
        // 创建配置实例
        WebSocketConfig config = new WebSocketConfig();
        
        // 测试ServerEndpointExporter Bean创建
        ServerEndpointExporter exporter = config.serverEndpointExporter();
        
        // 验证结果
        assertNotNull(exporter);
        assertTrue(exporter instanceof ServerEndpointExporter, "exporter应该是ServerEndpointExporter类型");
    }

    @Test
    void testRegisterWebSocketHandlers() {
        // 创建配置实例
        WebSocketConfig config = new WebSocketConfig();
        
        // 创建模拟的WebSocketHandlerRegistry
        WebSocketHandlerRegistry registry = mock(WebSocketHandlerRegistry.class);
        
        // 执行测试
        assertDoesNotThrow(() -> {
            config.registerWebSocketHandlers(registry);
        });
        
        // 验证registry被调用（虽然当前实现中没有注册任何handler）
        // 这里主要是验证方法不会抛出异常
    }

    @Test
    void testWebSocketConfigInstantiation() {
        // 测试配置类可以正常实例化
        WebSocketConfig config = new WebSocketConfig();
        assertNotNull(config);
    }

    @Test
    void testServerEndpointExporterMultipleCalls() {
        // 测试多次调用返回不同的实例
        WebSocketConfig config = new WebSocketConfig();
        
        ServerEndpointExporter exporter1 = config.serverEndpointExporter();
        ServerEndpointExporter exporter2 = config.serverEndpointExporter();
        
        // 验证返回的是不同的实例（因为每次调用都会创建新的Bean）
        assertNotSame(exporter1, exporter2);
        
        // 但都应该是ServerEndpointExporter类型
        assertTrue(exporter1 instanceof ServerEndpointExporter, "exporter1应该是ServerEndpointExporter类型");
        assertTrue(exporter2 instanceof ServerEndpointExporter, "exporter2应该是ServerEndpointExporter类型");
    }

    @Test
    void testServerEndpointExporterProperties() {
        // 测试ServerEndpointExporter的基本属性
        WebSocketConfig config = new WebSocketConfig();
        ServerEndpointExporter exporter = config.serverEndpointExporter();
        
        // 验证exporter不为null
        assertNotNull(exporter);
        
        // 验证类型
        assertTrue(exporter instanceof ServerEndpointExporter, "exporter应该是ServerEndpointExporter类型");
    }

    @Test
    void testRegisterWebSocketHandlersWithNullRegistry() {
        // 测试传入null registry的情况
        WebSocketConfig config = new WebSocketConfig();
        
        // 应该不会抛出异常（当前实现中没有使用registry）
        assertDoesNotThrow(() -> {
            config.registerWebSocketHandlers(null);
        });
    }

    @Test
    void testWebSocketConfigAnnotation() {
        // 测试配置类的注解
        WebSocketConfig config = new WebSocketConfig();
        
        // 验证类上有@Configuration注解
        assertTrue(config.getClass().isAnnotationPresent(org.springframework.context.annotation.Configuration.class));
        
        // 验证类上有@EnableWebSocket注解
        assertTrue(config.getClass().isAnnotationPresent(org.springframework.web.socket.config.annotation.EnableWebSocket.class));
    }

    @Test
    void testWebSocketConfigImplementsInterface() {
        // 测试配置类实现了正确的接口
        WebSocketConfig config = new WebSocketConfig();
        
        // 验证实现了WebSocketConfigurer接口
        assertTrue(config instanceof org.springframework.web.socket.config.annotation.WebSocketConfigurer);
    }

    @Test
    void testServerEndpointExporterBeanAnnotation() throws NoSuchMethodException {
        // 测试serverEndpointExporter方法有@Bean注解
        WebSocketConfig config = new WebSocketConfig();
        
        // 获取方法
        java.lang.reflect.Method method = config.getClass().getMethod("serverEndpointExporter");
        
        // 验证方法上有@Bean注解
        assertTrue(method.isAnnotationPresent(org.springframework.context.annotation.Bean.class));
    }

    @Test
    void testRegisterWebSocketHandlersMethodAnnotation() throws NoSuchMethodException {
        // 测试registerWebSocketHandlers方法有@Override注解
        WebSocketConfig config = new WebSocketConfig();
        
        // 获取方法
        java.lang.reflect.Method method = config.getClass().getMethod("registerWebSocketHandlers", WebSocketHandlerRegistry.class);
        
        // 验证方法上有@Override注解
        assertTrue(method.isAnnotationPresent(Override.class));
    }

    @Test
    void testWebSocketConfigThreadSafety() throws InterruptedException {
        // 测试配置类的线程安全性
        WebSocketConfig config = new WebSocketConfig();
        
        int threadCount = 10;
        Thread[] threads = new Thread[threadCount];
        ServerEndpointExporter[] exporters = new ServerEndpointExporter[threadCount];
        
        // 创建多个线程同时调用serverEndpointExporter方法
        for (int i = 0; i < threadCount; i++) {
            final int index = i;
            threads[i] = new Thread(() -> {
                exporters[index] = config.serverEndpointExporter();
            });
        }
        
        // 启动所有线程
        for (Thread thread : threads) {
            thread.start();
        }
        
        // 等待所有线程完成
        for (Thread thread : threads) {
            thread.join();
        }
        
        // 验证所有线程都成功创建了exporter
        for (ServerEndpointExporter exporter : exporters) {
            assertNotNull(exporter);
            assertTrue(exporter instanceof ServerEndpointExporter, "exporter应该是ServerEndpointExporter类型");
        }
    }

    @Test
    void testWebSocketConfigMemoryUsage() {
        // 测试配置类的内存使用情况
        WebSocketConfig config = new WebSocketConfig();
        
        // 创建多个exporter实例
        ServerEndpointExporter[] exporters = new ServerEndpointExporter[100];
        
        for (int i = 0; i < 100; i++) {
            exporters[i] = config.serverEndpointExporter();
        }
        
        // 验证所有实例都创建成功
        for (ServerEndpointExporter exporter : exporters) {
            assertNotNull(exporter);
            assertTrue(exporter instanceof ServerEndpointExporter, "exporter应该是ServerEndpointExporter类型");
        }
    }

    @Test
    void testWebSocketConfigWithMockRegistry() {
        // 使用mock registry测试registerWebSocketHandlers方法
        WebSocketConfig config = new WebSocketConfig();
        WebSocketHandlerRegistry registry = mock(WebSocketHandlerRegistry.class);
        
        // 执行方法
        config.registerWebSocketHandlers(registry);
        
        // 验证方法执行完成（当前实现中没有调用registry的方法）
        // 这里主要是验证方法不会抛出异常
        verifyNoInteractions(registry);
    }
}
